var ws = null;
var url = '/spring-websocket-test/sockjs/echo';
var transports = [];
var path= "http://"+window.location.hostname+":"+window.location.port;

function open_socket(state) {
	if (state == 1) {
		connect()
	} else {
		disconnect()
	}
	function connect() {
		if (!url) {
			alert('Select whether to use W3C WebSocket or SockJS');
			return;
		}
		ws = new SockJS(path+"/webSocketServer/sockjs");
		ws.onopen = function() {
			console.log('Info: connection opened.');
			echo()
		};
		ws.onmessage = function(event) {
			var title = '';
			if($("#talk-dialog")!=null && $("#talk-dialog").length>0) {
				title = $("#talk-dialog").panel('options').title;
			}
			var data = JSON.parse(event.data);
			$("#online_count").html(data.count)
			var user = data.data;
			if (user != null) {
				var mm3 = $("#mm3")
				$("#mm3").empty()
				for (var i = 0; i < user.length; i++) {
					mm3
							.append(" <div  style=\"height: 20px;\"  onclick=\"talkialog({title:'"+user[i].name+"',width:'30%',height:'60%',url:'connect/add_01?username="+user[i].name+"',onClose:closTalk})\"><div class=\"menu-text\" style=\"height: 20px; line-height: 20px;\">"
									+ user[i].name
									+ "<span class=\"badge color-important\">"+user[i].unread+"</span></div><div>");
					
					if(user[i].unread!=0 && title.trim()!=user[i].name) {
						nitice(user[i].name,user[i].unread);
					}
				}
				$.parser.parse(mm3);
			}
		};
		ws.onclose = function(event) {
			console.log('Info: connection closed.');
			$("#online_count").html(0)
			$("#mm3").empty()
		};
	}
	function echo() {
		if (ws != null) {
			ws.send("你好");
		} else {
			alert('connection not established, please connect.');
		}
	}
	function disconnect() {
		if (ws != null) {
			ws.close();
			ws = null;
		}
	}
	function updateUrl(urlPath) {
		if (urlPath.indexOf('sockjs') != -1) {
			url = urlPath;
		} else {
			if (window.location.protocol == 'http:') {
				url = 'ws://' + window.location.host + urlPath;
			} else {
				url = 'wss://' + window.location.host + urlPath;
			}
		}
	}
	function updateTransport(transport) {
		transport == 'all'
		transports = (transport == 'all') ? [] : [ transport ];
	}
}


//talk
var ws_talk = null;
function connect() {
	if (!url) {
		alert('Select whether to use W3C WebSocket or SockJS');
		return;
	}
	ws_talk = new SockJS(path+"/talk/sockjs");

	ws_talk.onopen = function() {
		console.log('Info: connection opened.');
	};

	ws_talk.onmessage = function(event) {
		load_read();
	};

	ws_talk.onclose = function(event) {
		console.log('Info: connection closed.');
	};
}

function echo() {
	if (ws_talk != null) {
		ws_talk.send("你好");
	} else {
		alert('connection not established, please connect.');
	}
}

function disconnect() {
	if (ws_talk != null) {
		ws_talk.close();
		ws_talk = null;
	}
}

function updateUrl(urlPath) {
	if (urlPath.indexOf('sockjs') != -1) {
		url = urlPath;
	} else {
		if (window.location.protocol == 'http:') {
			url = 'ws://' + window.location.host + urlPath;
		} else {
			url = 'wss://' + window.location.host + urlPath;
		}
	}
}

function updateTransport(transport) {
	transport == 'all'
	transports = (transport == 'all') ? [] : [ transport ];
}

function closTalk() {
	ws_talk.close();
	ws_talk = null;
}

function nitice(user,count) {
	setTimeout(function(){this.focus();this.blur();},2000);
	if (window.Notification) {
	        if (Notification.permission == "granted") {
	            popNotice(user,count);
	        } else if (Notification.permission != "denied") {
	            Notification.requestPermission(function (permission) {
	              popNotice(user,count);
	            });
	        }
	    
	     function popNotice(user,count) {
	        if (Notification.permission == "granted") {
	            var notification = new Notification(user, {
	                body: '您有'+count+'未读消息',
	                icon: '/base/images/head.jpg',
	                tag:'msg'+user,
	                renotify:true,
	            });
	            notification.onclick = function() {
	            	talkialog({title:user,width:'30%',height:'60%',url:'connect/add_01?username='+user,onClose:closTalk})
	                notification.close();    
	            };
	        }    
	    };
	} else {
	    alert('浏览器不支持Notification');    
	}
}